package cn.liberals.MQ.tran;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象，指定生产者组名
        TransactionMQProducer producer = new TransactionMQProducer("tran_group_producer");
        // 2.连接mq
        producer
                .setNamesrvAddr("127.0.0.1:9876");
        // 3.指定事务监听器
        producer.setTransactionListener(new MyTranListener());
        // 3.启动生产者
        producer.start();
        // 5.发送事务消息
        Message message = new Message("hello_topic", "hello_tags", "事务不是失误留下来的食物！".getBytes(StandardCharsets.UTF_8));
        SendResult sendResult = producer.sendMessageInTransaction(message,null);// 4.3.打印消息发送结果
        System.out.println(sendResult);
        // 5.关闭生产者
        producer.shutdown();
    }
}

